<html>
<head><title>Nicofarre test</title></head>
<body style="margin: 0; cursor: none">
<script src="../tma.js"></script>
<script src="mv_common.js"></script>
<script>
tma.extlibs = [ 'ext/gl-matrix.js', 'ext/mv/MajVj.js' ];
tma.onload = function () {
  var r = 1;
  var width = 1920 / r;
  var height = 1080 / r;
  var vj = new MajVj(width, height, true);
  var fftController = { volume: [1.0] };
  MajVj.loadAllPlugins().then(_(function () {
    var snd = vj.create('misc', 'sound', { delay: 0.0 });
    snd.fetch('data/bustling.mp3', true);
    var fft = new Float32Array(snd.getFftCount());
    var fftCalc = function (fft) {
      var size = fft.length;
      var sum = 0.0;
      for (var i = 0; i < size; ++i)
        sum += snd.normalizeFrequencyData(fft[i]);
      return sum / size;
    }
    var stage = vj.create('frame', 'nicofarre',
                            { led: MajVj.frame.nicofarre.LED_STAGE,
                              frames: [
                                { name: 'morphere', options:
                                  { controller: fftController } } ] });
    var fx = vj.create('effect', 'rgb', { controller: fftController });
    var fxb = vj.screen().createFrameBuffer(width, height);
    var emu = vj.create('effect', 'nicofarre');
    emu.setController(controller);
    var fbo = vj.screen().createFrameBuffer(width, height);
    vj.run(function (delta) {
      snd.getFloatFrequencyData(fft);
      fftController.volume[0] = fftCalc(fft);
      fxb.bind();
      vj.screen().setAlphaMode(false);
      vj.screen().fillColor(0.0, 0.0, 0.0, 1.0);
      stage.draw(delta);
      if (emulate)
        fbo.bind();
      else
        vj.screen().bind();
      vj.screen().fillColor(0.0, 0.0, 0.0, 1.0);
      fx.draw(delta, fxb.texture);
      if (emulate) {
        vj.screen().bind();
        vj.screen().setAlphaMode(false);
        emu.draw(delta, fbo.texture);
      }
      vj.screen().flush();
    });
    window.onresize = function () {
      vj.onresize();
      emu.onresize(vj.aspect());
    };
  }), function (e) { console.error('plugin load error: ' + e.stack); });
};
</script>
</body>
</html>
